#pragma once


#include "Comm.hpp"



void BubbleSort(int* a, int n)
{
	for (int i = 0; i < n - 1; ++i)
	{
		bool exchange = false;
		for (int j = 1; j < n - i; ++j)
		{
			if (a[j - 1] > a[j])
			{
				std::swap(a[j - 1], a[j]);
				exchange = true;
			}
		}
		if (!exchange) break;
	}
}


void InsertSort(int* a, int n)
{
	for (int i = 0; i < n - 1; ++i)
	{
		int end = i;
		int tmp = a[end + 1];
		while (end >= 0)
		{
			if (tmp < a[end]) a[end + 1] = a[end];
			else break;
			--end;
		}
		a[end + 1] = tmp;
	}
}


void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; ++i)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (tmp < a[end]) a[end + gap] = a[end];
				else break;
				end -= gap;
			}
			a[end + gap] = tmp;
		}
	}
}


void SelectSort(int* a, int n)
{
	int left = 0, right = n - 1;
	while (left < right)
	{
		int maxi = left, mini = left;
		for (int i = left + 1; i <= right; ++i)
		{
			if (a[i] < a[mini]) mini = i;
			if (a[i] > a[maxi]) maxi = i;
		}
		std::swap(a[left], a[mini]);
		if (maxi == left) maxi = mini;
		std::swap(a[right], a[maxi]);
		++left, --right;
	}
}



void HeapSort(int* a, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; --i)
		AdjustDown(a, n, i);
	
	int end = n - 1;
	while (end > 0)
	{
		std::swap(a[0], a[end]);
		AdjustDown(a, end, 0);
		--end;
	}
}



// 6, 1, 2, 7, 9, 3, 4, 5, 10, 8
void QuickSort(int* a, int begin, int end)
{
	if (end <= begin) return;

	int midi = GetMidi(a, begin, end);
	std::swap(a[begin], a[midi]);
	
	int keyi = PartSort3(a, begin, end);
	
	QuickSort(a, begin, keyi - 1);
	QuickSort(a, keyi + 1, end);
}


void QuickSortNonRecursion(int* a, int begin, int end)
{
	std::stack<std::pair<int, int>> st;
	st.push({ begin, end });
	
	while (st.size() > 0)
	{
		int left = st.top().first;
		int right = st.top().second;
		st.pop();

		int midi = GetMidi(a, left, right);
		std::swap(a[left], a[midi]);
		
		int keyi = PartSort3(a, left, right);

		if (left < keyi - 1) st.push({ left, keyi - 1 });
		if (keyi + 1 < right) st.push({ keyi + 1, right });
	}
}




void _MergeSort(int* a, int begin, int end, int* tmp)
{
	if (begin >= end) return;
	
	int midi = (begin + end) / 2;
	_MergeSort(a, begin, midi, tmp);
	_MergeSort(a, midi + 1, end, tmp);
	
	int i = begin;
	int cur_f = begin;
	int cur_b = midi + 1;
	while (cur_f <= midi && cur_b <= end)
	{
		if (a[cur_f] < a[cur_b])
			tmp[i++] = a[cur_f++];
		else
			tmp[i++] = a[cur_b++];
	}
	while (cur_f <= midi) tmp[i++] = a[cur_f++];
	while (cur_b <= end) tmp[i++] = a[cur_b++];
	memmove(a + begin, tmp + begin, (end - begin + 1) * sizeof(int));
}

void MergeSort(int* a, int n)
{
	int* tmp = new int[n] {0};
	if (tmp == nullptr) std::cout << "new error\n" << std::endl;
	_MergeSort(a, 0, n - 1, tmp);
	delete[] tmp;
}

//  6, 1, 2, 6, 7, 9, 3, 4, 5, 6, 10, 8
void MergeSortNonRecursion(int* a, int n)
{
	int* tmp = new int[n] {0};
	if (tmp == nullptr) std::cout << "new error\n" << std::endl;
	
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += 2 * gap)
		{
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;

			if (end1 >= n || begin2 >= n) break;
			if (end2 >= n) end2 = n - 1;

			int j = begin1;
			int cur_f = begin1, cur_b = begin2;
			while (cur_f <= end1 && cur_b <= end2)
			{
				if (a[cur_f] < a[cur_b]) tmp[j++] = a[cur_f++];
				else tmp[j++] = a[cur_b++];
			}
			while (cur_f <= end1) tmp[j++] = a[cur_f++];
			while (cur_b <= end2) tmp[j++] = a[cur_b++];
			memmove(a + begin1, tmp + begin1, sizeof(int) * (end2 - begin1 + 1));
		}
		gap *= 2;
	}

	delete[] tmp;
}